﻿using CKS.Core.Framework;
using FreeSql;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ZR.Model.Models.ProductionControl;

namespace CKS.PLC.Action.Subscription.OP70
{
    /// <summary>
    /// 合模操作保存70站数据
    /// 重写after操作额外记录70站合工装最新数据
    /// </summary>
    public class DB3011_Work01Action : SaveProcessingResults
    {
        public DB3011_Work01Action(DecodeValue _value) : base(_value)
        {
        }

        public override AfterSaveResult AfterSaved(SaveArg e)
        {

            DcOp70Record dc = new DcOp70Record()
            {
                FixtureId = Value.GetLinkData(1).ToString(),
                CoverCode = Value.GetLinkData(2).ToString(),
                BottomPlateCode = Value.GetLinkData(3).ToString(),
                MaxPressure = Value.GetLinkData(4).ToDecimal(),
                MaxDisplacement = Value.GetLinkData(5).ToDecimal(),
                WeldingWireGap = Value.GetLinkData(6).ToDecimal(),
                PressingHeight = Value.GetLinkData(7).ToDecimal(),
                MisalignmentX = Value.GetLinkData(8).ToDecimal(),
                MisalignmentY = Value.GetLinkData(9).ToDecimal(),
                IndentationDefect = Value.GetLinkData(10).ToDecimal(),
                ToolingGapUpper = Value.GetLinkData(11).ToDecimal(),
                ToolingGapLower = Value.GetLinkData(12).ToDecimal(),
                TotalResult = Value.GetLinkData(13).ToInt(),
                CreatedTime = DateTime.Now,
                UpdateTime = DateTime.Now,
            };
          //  conn.Insert(dc).ExecuteAffrows();

            //更新工装条码信息
            var fixture = conn.Select<CksFixtureinfo>().Where(a => a.FixtureId == dc.FixtureId.ToInt() ).First();
            if(fixture == null)
            {
                fixture = new CksFixtureinfo();
                fixture.MaxUsageCount = 200;
                fixture.Id = conn.Insert(fixture).ExecuteIdentity().ToInt();
               
            }
            fixture.FixtureId = dc.FixtureId.ToInt();
            fixture.CoverCode = dc.CoverCode;
            fixture.BottomPlateCode = dc.BottomPlateCode;
            fixture.Status = 0;
            fixture.CellId = 0;
            fixture.ShelfId = 0;
            
            conn.Update<CksFixtureinfo>().SetSource(fixture).ExecuteAffrows();
            return base.AfterSaved(e);
        }

       
    }
}
